<template>
  <view class="y-card">
    <view class="y-flex-between">
      <view class="y-title">患者信息</view>
      <view v-if="selectUseable" class="y-btn-ghost y-btn-ghost-primary" @click="navSelect">选择</view>
    </view>
    <view class="y-flex-between y-margin-top">
      <view class="y-must y-label">姓名</view>
      <input class="y-form-input y-bg-grey y-flex1" maxlength="12" placeholder="输入被照护人姓名" v-model="patientInfo.name" @input="bindInputChange" />
    </view>
    <view class="y-flex-between y-margin-top sex">
      <view class="y-must y-label">性别</view>
      <!-- $color-primary: #BF375A; -->
      <radio-group class="y-flex1" @change="bindRadioChange">
        <radio value="1" :checked="patientInfo.sex === '1'" color="#BF375A" class="y-margin-right">男</radio>
        <radio value="0" :checked="patientInfo.sex === '0'" color="#BF375A">女</radio>
      </radio-group>
    </view>
    <view class="y-flex-between y-margin-top">
      <view class="y-label y-must">年龄</view>
      <input class="y-form-input y-bg-grey y-flex1" maxlength="3" placeholder="请输入照护人年龄" v-model="birthday" @input="bindBirthdayInputChange" type="number" @blur="correctAge"/><view class="y-padding">
        岁
      </view>
    </view>
  </view>
</template>

<script>
  import util from '@/lib/util.js';
export default {
  props: { selectUseable: { type: Boolean, default: false } },
  data() {
    return {
      patientInfo: {
        sex: '0',
        birthday:  ''
      },
      birthday:  ''
    };
  },
  selectedPatient: {},
  methods: {
    correctAge(e){
      Number(e.detail.value)<1&&(this.birthday='',this.patientInfo.birthday='',uni.toast('年龄必须大于0'))
    },
    bindBirthdayInputChange(e) {
      this.patientInfo.birthday = e.detail.value;
      this.birthday = e.detail.value;
      this.patientMd5();
    },
    bindInputChange() {
      this.patientMd5();
    },
    bindRadioChange(e) {
      console.log(e);
      this.patientInfo.sex = e.target.value;
      this.patientMd5();
    },

    patientMd5(init) {
      let { name, sex, birthday, id } = this.patientInfo;
      birthday = birthday || '';
      // birthday = birthday.toString().substring(0, 10);
      const md5 = escape(name + sex + birthday);
      init ? (this.$options.selectedPatient = { md5, name, sex, birthday, id }) : (this.patientInfo.id = this.$options.selectedPatient.md5 === md5 ? id : null);
    },
    navSelect() {
      const that = this;
      uni.navigateTo({
        url: '/pages/common/patientList/patientList?readyOnly=false',
        events: {
          comeBackData(data) {
            data.sex += '';
            that.patientInfo = data;
            that.birthday = data.age;
            that.patientMd5(true);
          }
        }
      });
    }
  }
};
</script>

<style></style>
